﻿using System;
using System.Collections.Generic;
using DMS.BaseClass;
using DMS.Business.Modules.SupplierManagement;
using DMS.Business.Utility;

namespace DMS.Web.Modules.SupplierManagement
{
    public partial class SupplierAccountForm : System.Web.UI.Page
    {
        private string _id = "";
        private ActionType _action;


        protected void Page_Load(object sender, EventArgs e)
        {
             _id = Request["id"];
            _action = (ActionType)Enum.Parse(typeof(ActionType), Request["action"].ToString());

            

            if (!IsPostBack)
            {
                InitControls();
                InitData();
            }
        }

        private void InitControls()
        {
            if (_id != "")
            {
                trPassword.Visible = false;
                trPassword2.Visible = false;
            }
        }

        private void InitData()
        {
            //InitSuppliers();
            //InitProjectTypes();

            if (_id != "")
            {
                SupplierAccount supplier = SupplierHelper.GetAccountByID(_id);
                FormBinder.BindObjectToControls(supplier, divContent);
            }
        }
       
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid)
            {
                return;
            }

            try
            {
                SupplierAccount account;
                if (_action == ActionType.New)
                {
                    account = new SupplierAccount();
                }
                else
                {
                    account = SupplierHelper.GetAccountByID(_id);
                }
                FormBinder.BindControlsToObject(account, divContent);
                account.ProjectTypeName = ProjectTypeID.SelectedItem.Text;
                account.Supplier = SupplierID.SelectedItem.Text;

                //新建时密码
                if (_action == ActionType.New)
                {
                    account.PasswordCode = account.EncryptPWD(txtPassword.Text);
                }

                account.Save();

                ClientHelper.Instance.CloseWindow("保存成功", "ok", RefreshType.OpenDialog);
            }
            catch (Exception ex)
            {
                string code = LogHelper.LogException(ex);
                string msg = string.Format("操作失败，请联系管理员。\\r\\n错误码：{0}", code);
                ClientHelper.Instance.Alert(msg);
            }
        }
    }
}